package edu.ieslaferreria.creditsintesis.builders;

import com.jme.scene.state.LightState;
import com.jmex.terrain.TerrainPage;
import java.net.URL;


import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.light.DirectionalLight;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.state.CullState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import edu.ieslaferreria.creditsintesis.logicgame.Parameter;
import edu.ieslaferreria.creditsintesis.logicgame.actions.ConfigurationMapEvent;

/**
 * Started Date: Aug 19, 2004<br><br>
 *
 * This program introduces jME's terrain utility classes and how they are used.  It
 * goes over ProceduralTextureGenerator, ImageBasedHeightMap, MidPointHeightMap, and
 * TerrainBlock.
 * Es un terreno plano conseguido con el piecesofMap
 *
 * @author Josep Cañellas
 */
public class PlaneTerrain implements TerrainManager {
    String name;
    private ConfigurationMapEvent event;

    int totalScale = 10;
    int totalSizeMap = 33;
    float[]  map = new float[this.totalSizeMap*totalSizeMap];



    public TerrainPage buildTerrain(Node rootNode,String name, int blocksize, int size, Vector3f vector3f, String modelName) {
        TerrainPage res;
        this.event= new ConfigurationMapEvent(modelName);

        PiecesOfMap contructorMaps = new PiecesOfMap();
        CullState cs = DisplaySystem.getDisplaySystem().getRenderer().createCullState();
        cs.setCullFace(CullState.Face.Back);
        cs.setEnabled(true);
        rootNode.setRenderState(cs);
    // This will be the texture for the terrain.

        contructorMaps.addNewPieceOnMap(this.map,PiecesOfMap.PLAIN_0,PiecesOfMap.G_0,0,0,this.totalSizeMap);
        contructorMaps.addNewPieceOnMap(this.map,PiecesOfMap.PLAIN_0,PiecesOfMap.G_0,1,0,this.totalSizeMap);
        contructorMaps.addNewPieceOnMap(this.map,PiecesOfMap.PLAIN_0,PiecesOfMap.G_0,1,1,this.totalSizeMap);
        contructorMaps.addNewPieceOnMap(this.map,PiecesOfMap.PLAIN_0,PiecesOfMap.G_0,0,1,this.totalSizeMap);
        this.totalSizeMap=size;
         res = new TerrainPage(name, blocksize, this.totalSizeMap, vector3f,this.map );

 // Add the texture

        // Attach the terrain TriMesh to our rootNode
         this.setTexture(res);
         rootNode.attachChild(res);

         this.getEvent().addParameter("Blocksize",new Parameter(blocksize));
         this.getEvent().addParameter("Size",new Parameter(size));
         this.getEvent().addParameter("Vector3f",new Parameter(vector3f));
         this.getEvent().addParameter("Map",new Parameter(this.map));
        return res;

    }
    public void setSizeMap(int size){
        this.map = new float[size*size];
        this.totalSizeMap = size;
    }

    public Sky buildSky(Node rootNode, TerrainPage terrain) {
        Sky dome = new Sky();
        rootNode.attachChild(dome);
        return dome;
    }

    public LightState buildLighting() {
            /** Set up a basic, default light. */
        DirectionalLight light = new DirectionalLight();
        light.setDiffuse(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
        light.setAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, 1.0f));
        light.setDirection(new Vector3f(1,-1,0));
        light.setEnabled(true);

          /** Attach the light to a lightState and the lightState to rootNode. */
        LightState lightState = DisplaySystem.getDisplaySystem().getRenderer()
                                                              .createLightState();
        lightState.setEnabled(true);
        lightState.attach(light);
        return lightState;
    }

//  create a default Terrain
    public TerrainPage buildTerrain(Node rootNode) {
        return this.buildTerrain(rootNode,"defaultTerrain",17,this.totalSizeMap,new Vector3f(10f, 10f, 10f),"PlaneTerrain");
    }

    /**
     * @return the event
     */
    public ConfigurationMapEvent getEvent() {
        return event;
    }

    public TerrainPage buildRemoteTerrain(Node rootNode, String name, int blocksize, int size, Vector3f vector3f, float[] map, String modelTerrainName) {
        TerrainPage terrain;
        terrain= new TerrainPage(name, blocksize, this.totalSizeMap, vector3f,map);
        this.setTexture(terrain);
        rootNode.attachChild(terrain);
        return terrain;

    }

    private void setTexture(TerrainPage terrain){
        TextureState ts = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
        URL grass=CaveMazeTerrain.class.getClassLoader().getResource(
            "texture/grassb.png");
        ts.setTexture(TextureManager.loadTexture(grass,
                Texture.MinificationFilter.BilinearNearestMipMap,
                Texture.MagnificationFilter.Bilinear));
        terrain.setRenderState(ts);


        // Give the terrain a bounding box.
        terrain.setModelBound(new BoundingBox());
        terrain.updateModelBound();

    }


}
